<!-- notice表单弹窗 -->
<form id="notice-form" lay-filter="notice-form" class="layui-form model-form">
    <input name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">通知标题</label>
        <div class="layui-input-block">
            <input name="ntctle" placeholder="请填写标题" type="text" class="layui-input" maxlength="20"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">通知内容</label>
        <div class="layui-input-block">
            <textarea id="contnt" name="contnt"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">截止时间</label>
        <div class="layui-input-block">
            <input name="clsdat" id="clsdat" placeholder="请选择截止时间" type="text" class="layui-input"
                   required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block">
<!--            <select name="roleId" xm-select="roleId" id="xm-select" lay-verify="required">-->
<!--            </select>-->
            <div id="xm-select"></div>
        </div>
    </div>
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="notice-form-submit" lay-submit>保存</button>
    </div>
</form>

<!--<script src="../../module/xm-select/xm-select.js"></script>-->
<script>
    layui.use(['layer', 'admin', 'form','xmSelect','tinymce'], function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var laydate = layui.laydate;
        var xmSelect = layui.xmSelect;
        var tinymce = layui.tinymce;
        tinymce.render({
            elem: "#contnt",
            height: '200px'
            // 支持tinymce所有配置
        },(opt, edit)=>{
            // 加载完成后回调 opt 是传入的所有参数
            // edit是当前编辑器实例，等同于t.get返回值
        });
        //渲染日期选择器
        laydate.render({
            elem: "#clsdat",
            type: 'datetime'
        })
        //渲染form表单中的单选框
        form.render('radio');
        //加载层
        layer.load(2);
        // 获取所有角色
        admin.req('api-user/roles', {}, function (data) {
            console.log(data);
            //关闭加载层
            layer.closeAll('loading');
            if (0 == data.code) {
                // 渲染多选下拉框
                var roleSelectData = new Array();
                for (var i = 0; i < data.data.length; i++) {
                    roleSelectData.push({name: data.data[i].name, value: data.data[i].id});
                }
                console.log(roleSelectData);
                //全选, 清空, 反选
                var xmrole =  xmSelect.render({
                    el: '#xm-select',
                    language: 'zn',
                    toolbar: {
                        show: true,
                        list: [ 'ALL', 'CLEAR', 'REVERSE' ]
                    },
                    data: roleSelectData
                })
                // formSelects.data('roleId', 'local', {arr: roleSelectData});

                // // 回显notice数据
                // var notice = admin.getTempData('t_notice');
                // console.log(notice);
                // $('#notice-form').attr('method', 'POST');
                // if (notice) {
                //     form.val('notice-form', notice);
                //     $('#notice-form').attr('method', 'POST');
                //     var rds = new Array();
                //     for (var i = 0; i < notice.roles.length; i++) {
                //         rds.push(notice.roles[i].id);
                //     }
                //     formSelects.value('roleId', rds);
                // }

            } else {
                layer.msg('获取角色失败', {icon: 2, time: 500});
            }
        }, 'GET');

        // 表单提交事件
        form.on('submit(notice-form-submit)', function (data) {
            layer.load(2);
            console.log(data);
        //     admin.req('api-user/notice/publishOrUpdateNotice', JSON.stringify(data.field), function (data) {
        //         layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
        //             admin.finishPopupCenter();
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                }
        //     }, $('#notice-form').attr('method'));
            return false;
        });
    });
</script>